package com.qf.controller;

import com.qf.pojo.User;
import com.qf.pojo.UserList;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttributes;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/Param")
public class ParamController {

    @RequestMapping("/test01")
    public String test01(Integer id, String name, Boolean sex, Date birth) {
        System.out.println("test01");
        System.out.println("id=" + id + " name=" + name + " sex=" + sex + " birth=" + birth);
        return "hello";
    }

    @RequestMapping("/test02")
    public String test02(User user) {
        System.out.println("test02");
        System.out.println(user);
        return "hello";
    }

    @RequestMapping("/test03")
    public String test03(User user) {
        System.out.println("test03");
        System.out.println(user);
        return "hello";
    }

    @RequestMapping("/test04")
    public String test04(UserList userList) {
        System.out.println("test04");
        for (User user : userList.getUsers()) {
            System.out.println(user);
        }
        return "hello";
    }

    @RequestMapping("/test05/{id}")
    public String test05(@PathVariable Integer id) {
        System.out.println("test05");
        System.out.println("id:" + id);
        return "hello";
    }

    @RequestMapping("/test06/{id}/{name}")
    public String test06(@PathVariable Integer id, @PathVariable("name") String name) {
        System.out.println("test06");
        System.out.println("id:" + id + " name:" + name);
        return "hello";
    }

    @RequestMapping("/test")
    public String testData(HttpSession session, HttpServletRequest request, Integer id) {
        session.setAttribute("user", new User(1, "tom", true, new Date()));
        request.setAttribute("age", 18);
        request.setAttribute("birth", 1997 - 11 - 11);
        request.setAttribute("user", Arrays.asList(new User(), new User()));
        return "forward:/WEB-INF/jsp/hello.jsp";

    }

    @RequestMapping("/testModel")
    public String testModel(Model model) {
        model.addAttribute("age", 52);

        return "hello";
    }

    @RequestMapping("/test7")
    public ModelAndView test7(){
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.setViewName("forward:/WEB-INF/jsp/hello.jsp");
        modelAndView.addObject("name","tom");
        return modelAndView;
    }



}
